import time
from common.basepage import Basepage
from selenium.webdriver.common.by import By
from common.yaml_handler import result

Host = result["Host"]


class RolePages(Basepage):
    url = Host + '/#/project/role'
    """管理元素定位"""
    # 新增角色组按钮
    role_group_button = (By.CLASS_NAME, "el-icon-plus")
    # 角色组名称输入框
    role_groip_name = (By.XPATH, "//label[contains(text(),'角色组名称')]//following-sibling::div//input")
    # 排序输入框
    sort = (By.XPATH, "//div[@class = 'row_height el-col el-col-12']//input")
    # 新增角色组确认按钮
    role_group_confirm_button = (By.CLASS_NAME, "rightbutton")
    # 请输入部门名称提示
    please_enter_the_department = (By.CLASS_NAME, "el-form-item__error")
    # 操作成功提示框
    prompt_box = (By.CLASS_NAME, "el-message__content")
    # 修改角色组按钮
    edit_role_group_button = (By.XPATH, "//tr[@class = 'el-table__row']//td[3]//div//div//div[2]")
    # 删除角色组按钮
    delete_role_group_button = (By.XPATH, "//tr[@class = 'el-table__row']//td[3]//div//div//div[4]")
    # 删除弹窗确认按钮
    delete_tips_confirm_button = (By.CLASS_NAME, "ok-btn")
    # 新增角色按钮
    add_role_button = (By.XPATH, "//tr[@class = 'el-table__row']//td[3]//div//div//div[1]")
    # 新增角色名称
    add_role_name = (By.XPATH, "//div[@class='el-form-item is-required']//input[@class='el-input__inner']")
    # 新增角色的备注
    add_role_remarks = (By.XPATH, "//textarea[@class = 'el-textarea__inner']")
    # 新增角色排序
    add_role_sort = (By.XPATH, "//div[@class = 'row-height el-col el-col-12']//input")
    # 勾选全部
    check_all = (By.XPATH, "//span[contains(text(), '勾选/取消所有业务权限（请慎用）')]/..//span")
    # 新增角色保存按钮
    add_role_preservation_button = (By.XPATH, "//button[@class='el-button confirmbutton el-button--default']")
    # 展示角色组按钮
    open_role_group = (By.XPATH, "//tr[2]//td[3]//div[1]//div[1]//div[2]")
    # 角色编辑按钮
    role_edit_button = (By.XPATH, "//tr[@class = 'el-table__row el-table__row--striped el-table__row--level-1']//td[3]//div[2]")
    # 邻里中心权限
    neighborhood_authority = (By.XPATH, "//span[contains(text(), '邻里中心') and @class]/..//span")

    def add_role_group(self, name, numb="1"):
        self.click(self.role_group_button)
        self.fill(self.role_groip_name, name)
        time.sleep(1)
        self.fill(self.sort, numb)
        time.sleep(1)
        self.click(self.role_group_confirm_button)
        time.sleep(1)
        el = self.driver.find_element(*self.prompt_box)
        text = el.text
        return text


    def role_group_add_without_departmentname(self, numb="1"):
        self.click(self.role_group_button)
        self.fill(self.sort, numb)
        time.sleep(1)
        self.click(self.role_group_confirm_button)
        time.sleep(1)
        el = self.driver.find_element(*self.please_enter_the_department)
        text = el.text
        return text

    def role_group_add_departmentname_1_character(self, name, numb):
        self.click(self.role_group_button)
        self.fill(self.role_groip_name, name)
        time.sleep(1)
        self.fill(self.sort, numb)
        time.sleep(1)
        self.click(self.role_group_confirm_button)
        time.sleep(1)
        el = self.driver.find_element(*self.please_enter_the_department)
        text = el.text
        return text

    def edit_role_group_sucess(self, name):
        self.click(self.edit_role_group_button)
        time.sleep(1)
        self.clear(self.role_groip_name)
        self.fill(self.role_groip_name, name)
        time.sleep(1)
        self.click(self.role_group_confirm_button)
        time.sleep(1)
        el = self.driver.find_element(*self.prompt_box)
        text = el.text
        return text

    def delete_role_group_success(self):
        self.click(self.delete_role_group_button)
        time.sleep(2)
        self.click(self.delete_tips_confirm_button)
        time.sleep(2)
        el = self.driver.find_element(*self.prompt_box)
        text = el.text
        return text

    def delete_role_group_fail(self, name, numb):
        self.click(self.role_group_button)
        self.fill(self.role_groip_name, name)
        time.sleep(1)
        self.fill(self.sort, numb)
        time.sleep(1)
        self.click(self.role_group_confirm_button)
        self.click(self.delete_role_group_button)
        time.sleep(2)
        self.click(self.delete_tips_confirm_button)
        time.sleep(2)
        el = self.driver.find_element(*self.prompt_box)
        text = el.text
        return text

    def add_role_success(self, name, remarks, numb):
        self.click(self.add_role_button)
        self.fill(self.add_role_name, name)
        self.fill(self.add_role_remarks, remarks)
        self.fill(self.add_role_sort, numb)
        time.sleep(1)
        self.click(self.check_all)
        time.sleep(1)
        self.click(self.add_role_preservation_button)
        time.sleep(1)

    def edit_role_jurisdiction(self, name):
        self.click(self.open_role_group)
        self.click(self.role_edit_button)
        time.sleep(2)
        self.clear(self.add_role_name)
        time.sleep(2)
        self.fill(self.add_role_name, name)
        time.sleep(2)
        self.click(self.neighborhood_authority)
        self.click(self.add_role_preservation_button)









